package com.hsh.maoduo.service.impl;

import com.hsh.maoduo.constant.EMSPathConstants;
import com.hsh.maoduo.constant.common.DateConstants;
import com.hsh.maoduo.constant.common.ResultStrConstants;
import com.hsh.maoduo.pojo.other.ems.HkeSend;
import com.hsh.maoduo.pojo.other.ems.Note;
import com.hsh.maoduo.pojo.other.ems.SalesSlip;
import com.hsh.maoduo.pojo.other.ems.Transfer;
import com.hsh.maoduo.service.EMSService;
import com.hsh.maoduo.util.HuToolExcelUtil;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author hsh
 * @DateTime 2023/10/27 15:31
 **/
@Service
public class EMSServiceImpl implements EMSService {
    public String pddExcelWriter() {
        Date date = new Date();

        //读取当天所有的销售出库单文档
        List<String> salesSlipNames = findAllNameToday(EMSPathConstants.SALES_SLIP_IN_PATH_PRE
                , EMSPathConstants.SALES_SLIP_NAME_PRE + DateConstants.NORM_DATE_FORMAT.format(date));
        if (salesSlipNames.size()>0) {
            return ResultStrConstants.LIST_IS_NULL;
        }
        List<SalesSlip> salesSlipList = HuToolExcelUtil.redaExcels(EMSPathConstants.SALES_SLIP_IN_PATH_PRE
                , salesSlipNames, SalesSlip.class);

        //读取当天最新的销售出库单文档
        String transferNewestName = findNewestName(EMSPathConstants.TRANSFER_IN_PATH_PRE
                , EMSPathConstants.TRANSFER_IN_NAME_PRE );
        if ("".equals(transferNewestName)) {
            return ResultStrConstants.LIST_IS_NULL;
        }
        String transferName = EMSPathConstants.TRANSFER_IN_PATH_PRE + transferNewestName;
        List<Transfer> transferList = HuToolExcelUtil.redaExcel(transferName
                , Transfer.class);

        //销售出库单文档与销售出库单文档 均不为空且数据条数相等，则继续
        if (salesSlipList.isEmpty() || transferList.isEmpty()) {
            return ResultStrConstants.LIST_IS_NULL;
        }
        if (salesSlipList.size() != transferList.size()) {
            return ResultStrConstants.SIZE_IS_NOT_EQUALS;
        }
        //备注文档 集合
        List<Note> noteList = new ArrayList<>();
        //批量发货 集合
        List<HkeSend> hkeSendList = new ArrayList<>();
        hkeSendList.add(new HkeSend());

        //填写数据
        int size = salesSlipList.size();
        for (int i = 0; i < size; i++) {
            SalesSlip salesSlip = salesSlipList.get(i).init();
            Note note = new Note();
            HkeSend hkeSend = new HkeSend();
            Transfer transfer = transferList.get(i);

            String hkeExpressNo = transfer.getHkeExpressNo();
            //销售出库单
            salesSlip.setHkeExpressNo(hkeExpressNo);
            //备注文档
            note.setOrderNo(salesSlip.getOrderNo());
            note.setNote(hkeExpressNo);
            noteList.add(note);
            //批量导入发货
            hkeSend.setOrderNo(salesSlip.getOrderNo());
            hkeSend.setHkeExpressNo(hkeExpressNo);
            hkeSendList.add(hkeSend);
            //转单文档
            transfer.setEmsExpressNo(salesSlip.getEmsExpressNo());
            transfer.setDispatchCity(salesSlip.getCity());
            transfer.setSignForCity(salesSlip.getCity());
        }

        //写入表格
        try {
            HuToolExcelUtil.writeExcel(EMSPathConstants.SALES_SLIP_OUT_PATH_PRE
                            + EMSPathConstants.SALES_SLIP_NAME_PRE
                            + DateConstants.NORM_DATETIME_PATH_FORMAT.format(date)
                    , salesSlipList, SalesSlip.class);
            HuToolExcelUtil.writeExcel(EMSPathConstants.ORDER_NOTE_PATH_PRE
                            + EMSPathConstants.ORDER_NOTE_OUT_NAME_PRE
                            + DateConstants.NORM_DATETIME_PATH_FORMAT.format(date)
                    , noteList, Note.class);
            HuToolExcelUtil.writeExcel(EMSPathConstants.HKE_EXPRESS_PATH_PRE
                            + EMSPathConstants.HKE_EXPRESS_OUT_NAME_PRE
                            + DateConstants.NORM_DATETIME_PATH_FORMAT.format(date)
                    , hkeSendList, HkeSend.class);
            HuToolExcelUtil.writeExcel(EMSPathConstants.TRANSFER_OUT_PATH_PRE
                            + EMSPathConstants.TRANSFER_OUT_NAME_PRE
                            + transferNewestName
//                          + DateConstant.NORM_DATETIME_PATH_FORMAT.format(date)
                    , transferList, Transfer.class);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultStrConstants.WRITE_EXCEL_FAIL;
        }

        //删除初始表格
        HuToolExcelUtil.deleteExcels(EMSPathConstants.SALES_SLIP_IN_PATH_PRE, salesSlipNames);
        HuToolExcelUtil.deleteExcel(EMSPathConstants.TRANSFER_IN_PATH_PRE + transferNewestName);

        return String.valueOf(size);
    }

    /**
     * 查找路径pathPre下，名字包含字段namePre的所有文件路径
     *
     * @param pathPre
     * @param namePre
     * @return
     */
    private List<String> findAllNameToday(String pathPre, String namePre) {
        List<String> list = new ArrayList<>();
        File folder = new File(pathPre);
        String[] names = folder.list();
        if (Objects.isNull(names) || names.length == 0) {
            return list;
        }
        for (String name : names) {
            if (name.startsWith(namePre)) {
                list.add(name);
            }
        }
        return list;
    }

    /**
     * 获得修改时间最晚的
     * 该路径下包含该前缀的文件名称
     *
     * @param pathPre
     * @return
     */
    private String findNewestName(String pathPre, String namePre) {
        String name = "";
        File folder = new File(pathPre);
        long maxLastModified = 0L;
        File[] files = folder.listFiles();
        if (files == null) {
            return name;
        }
        for (File file : files) {
            if (file.getName().startsWith(namePre)) {
                maxLastModified = Math.max(file.lastModified(), maxLastModified);
                name = file.getName();
            }
        }
        return name;
    }
}
